﻿#include "xplaneConnect.h"


#ifdef WIN32
#include <Windows.h>
#endif


#include "Voice.h";
#include "skyThread.h"
#include "dataThread.h"    
#include "background.h"
#include "Checklists.h"
#include "PFD.h"
#include "MFD.h"
#include "ND.h"
#include "EWD.h"
#include "WD.h"
#include "OIS.h"
#include <thread>


void Voice1();

// 全局互斥锁，用于保护共享数据
std::mutex sharedDataMutex;

int main() {
    
    WindowParams window = initializeWindow();
    
    // 创建读取数据线程
    std::thread dataThread(xplaneDataThread);

    ViewState viewState;
    ULONGLONG last_time = GetTickCount64();
    ege_enable_aa(true);
    
    for (; is_run(); delay_fps(90)) {
        cleardevice();
       
        {
            std::lock_guard<std::mutex> lock(sharedData.mtx);

            // 计算时间差
            ULONGLONG current_time = GetTickCount64();
            float deltaTime = (current_time - last_time) / 1000.0f;
            last_time = current_time;
            
            // 输入处理
            handleMouseInput(viewState, window);
            handleKeyboardInput(viewState, deltaTime);
            
            // 运动更新
            updateMovement(viewState, deltaTime);
            reverseTransmission(sharedData.landing,sharedData.gear1);
             
            // 须更改部分(仪表盘的绘制)
            {
                // 一个背景
                auto [o_x, o_y, o_h] = calculateViewportParams(
                    viewState,
                    486.0f, 478.0f, 1250.0f,
                    window
                );
                setfillcolor(BLACK);
                ege_fillrect(o_x, o_y, o_h * 3.055,o_h);
                //setbkcolor(WHITE);

                // PFD（右边）
                auto [pfd_x, pfd_y, pfd_h] = calculateViewportParams(
                    viewState,
                    3262.0f, 812.0f, 385.0f,
                    window
                );

                if (pfd_h > 0) {
                    draw_PFD(pfd_x, pfd_y, pfd_h, sharedData.pitch, sharedData.roll, sharedData.speed, sharedData.height, sharedData.heading, sharedData.Chuispeed,sharedData.landing);
                    drawbkPFD(pfd_x, pfd_y, pfd_h);
                }

                // PFD(左边)
                auto [pfd2_x, pfd2_y, pfd2_h] = calculateViewportParams(
                    viewState,
                    1265.0f, 812.0f, 385.0f,
                    window
                );
                if (pfd2_h > 0) {
                    draw_PFD(pfd2_x, pfd2_y, pfd2_h, sharedData.pitch, sharedData.roll, sharedData.speed, sharedData.height, sharedData.heading, sharedData.Chuispeed,sharedData.landing);
                    drawbkPFD(pfd2_x, pfd2_y, pfd2_h);
                }

                //// ND
                {
                    // ND1
                    auto [nd1_x, nd1_y, nd1_h] = calculateViewportParams(
                        viewState,
                        1585.0f, 810.0f, 400.0f,
                        window
                    );
                    if (nd1_h > 0) {

                        int choice = 1;
                        if (sharedData.choice1 == 0)
                        {
                            drawLS(nd1_x, nd1_y, 525, nd1_h, nd1_x + (525 * nd1_h / 675) / 2, nd1_y + nd1_h / 2.5, nd1_h * 158 / 626, sharedData.headingAngle, choice, sharedData.GS, sharedData.TAS, sharedData.WinV, sharedData.WinD, sharedData.VOR1HL, sharedData.VOR2HL, sharedData.VOR1D, sharedData.VOR2D, sharedData.Rdirection, sharedData.VOR1, sharedData.VOR2);

                        }
                        if (sharedData.choice1 == 1)
                        {
                            drawVOR(nd1_x, nd1_y, 525, nd1_h, nd1_x + (525 * nd1_h / 675) / 2, nd1_y + nd1_h / 2.5, nd1_h * 158 / 626, sharedData.headingAngle, choice, sharedData.GS, sharedData.TAS, sharedData.WinV, sharedData.WinD, sharedData.VOR1HL, sharedData.VOR2HL, sharedData.VOR1D, sharedData.VOR2D, sharedData.Rdirection, sharedData.VOR1, sharedData.VOR2);
                        }
                        if (sharedData.choice1 == 2)
                        {
                            drawNAV(nd1_x, nd1_y, 525, nd1_h, nd1_x, nd1_y, 525 * nd1_h / 675, nd1_h, choice, sharedData.height, sharedData.headingAngle, sharedData.GS, sharedData.TAS, sharedData.WinV, sharedData.WinD, sharedData.VOR1HL, sharedData.VOR2HL, sharedData.VOR1D, sharedData.VOR2D, sharedData.Rdirection, sharedData.pitch, sharedData.VOR1, sharedData.VOR2);
                        }
                        if (sharedData.choice1 == 3)
                        {
                            drawND1(nd1_x, nd1_y, 525, nd1_h, 2, sharedData.headingAngle, sharedData.VOR1HL, sharedData.VOR2HL, sharedData.VOR1D, sharedData.VOR2D, sharedData.GS, sharedData.TAS, sharedData.WinV, sharedData.WinD, sharedData.height, sharedData.Rdirection, sharedData.pitch, sharedData.VOR1, sharedData.VOR2);
                        }
                        if (sharedData.choice1 == 4)
                        {
                            drawPLAN(nd1_x, nd1_y, 525, nd1_h, sharedData.headingAngle, sharedData.GS, sharedData.TAS, sharedData.WinV, sharedData.WinD);
                        }
                    }
                        // ND2
                        auto [nd2_x, nd2_y, nd2_h] = calculateViewportParams(
                            viewState,
                            2930.0f, 810.0f, 400.0f,
                            window
                        );
                        if (nd2_h > 0) {

                            int choice = 1;
                            if (sharedData.choice1 == 0)
                            {
                                drawLS(nd2_x, nd1_y, 525, nd1_h, nd2_x + (525 * nd1_h / 675) / 2, nd1_y + nd1_h / 2.5, nd1_h * 158 / 626, sharedData.headingAngle, choice, sharedData.GS, sharedData.TAS, sharedData.WinV, sharedData.WinD, sharedData.VOR1HL, sharedData.VOR2HL, sharedData.VOR1D, sharedData.VOR2D, sharedData.Rdirection, sharedData.VOR1, sharedData.VOR2);

                            }
                            if (sharedData.choice1 == 1)
                            {
                                drawVOR(nd2_x, nd1_y, 525, nd1_h, nd2_x + (525 * nd1_h / 675) / 2, nd1_y + nd1_h / 2.5, nd1_h * 158 / 626, sharedData.headingAngle, choice, sharedData.GS, sharedData.TAS, sharedData.WinV, sharedData.WinD, sharedData.VOR1HL, sharedData.VOR2HL, sharedData.VOR1D, sharedData.VOR2D, sharedData.Rdirection, sharedData.VOR1, sharedData.VOR2);
                            }
                            if (sharedData.choice1 == 2)
                            {
                                drawNAV(nd2_x, nd1_y, 525, nd1_h, nd2_x, nd1_y, 525 * nd1_h / 675, nd1_h, choice, sharedData.height, sharedData.headingAngle, sharedData.GS, sharedData.TAS, sharedData.WinV, sharedData.WinD, sharedData.VOR1HL, sharedData.VOR2HL, sharedData.VOR1D, sharedData.VOR2D, sharedData.Rdirection, sharedData.pitch, sharedData.VOR1, sharedData.VOR2);
                            }
                            if (sharedData.choice1 == 3)
                            {
                                drawND1(nd2_x, nd1_y, 525, nd1_h, 2, sharedData.headingAngle, sharedData.VOR1HL, sharedData.VOR2HL, sharedData.VOR1D, sharedData.VOR2D, sharedData.GS, sharedData.TAS, sharedData.WinV, sharedData.WinD, sharedData.height, sharedData.Rdirection, sharedData.pitch, sharedData.VOR1, sharedData.VOR2);
                            }
                            if (sharedData.choice1 == 4)
                            {
                                drawPLAN(nd2_x, nd2_y, 525, nd2_h, sharedData.headingAngle, sharedData.GS, sharedData.TAS, sharedData.WinV, sharedData.WinD);
                            }

                        }

                    
                    // //EWD
                    double angle = 0.0;//旋转角度
                    auto [ewd_x, ewd_y, ewd_h] = calculateViewportParams(
                        viewState,
                        2093.0f, 804.0f, 318.0f,
                        window
                    );
                    if (ewd_h > 0) {
                        drawEWD(ewd_x, ewd_y, ewd_h, ewd_h * 860 / 680, alpha, LV, angle);
                    }

                    // //WD
                    auto [sd_x, sd_y, sd_h] = calculateViewportParams(
                        viewState,
                        2410.0f, 804.0f, 322.0f,
                        window
                    );
                    if (sd_h > 0) {
                        WD(sd_x, sd_y, sd_h, sd_h * 860 / 680, LV);
                    }


                    // //背景的绘制
                    auto [x0, y0, h0] = calculateViewportParams(
                        viewState,
                        0.0f, 0.0f, 1440.0f,
                        window
                    );
                    if (h0 > 0) {
                        drawBackground(x0, y0, viewState);
                    }

                    // 在main循环中，绘制完成后添加：
                    Voice1();
                    setfont(30, 0, "Tahoma");
                    char fps_info[64];
                    sprintf(fps_info, "FPS: %.1f", getfps());
                    setcolor(EGERGB(255, 255, 255));
                    outtextxy(10, 10, fps_info);
                }
            }
            
               

        }
    }

    dataThread.join();
    //closeUDP(sock);
    return 0;
}




void Voice1() {
    
    if (sharedData.SpeedWD == 1&& sharedData.flag == 0) {
        sharedData.flag = 1;
        //const char* audioPath = "resource/sounds.rc/CSC.wav";
        const char* cmd_open = "open \"resource/sounds.rc/CSC.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);

        // 异步播放：添加 "notify" 标志
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
    }
    else if (sharedData.SpeedWD == 0) {
        sharedData.flag = 0;
    }

    

    if (sharedData.height <= 15 && sharedData.Chuispeed < 0 && sharedData.flag10 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/10.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag10 = 1;
    }
    else if (sharedData.height > 15) {
        sharedData.flag10 = 0;
    }

    if (sharedData.height >= 15 && sharedData.height <= 25 && sharedData.Chuispeed < 0 && sharedData.flag20 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/20.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag20 = 1;
    }
    else if (sharedData.height > 25) {
        sharedData.flag20 = 0;
    }

    // 按照前两个的逻辑补全后续条件判断，保持独立的if语句
    if (sharedData.height >= 25 && sharedData.height <= 35 && sharedData.Chuispeed < 0 && sharedData.flag30 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/30.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag30 = 1;
    }
    else if (sharedData.height > 35) {
        sharedData.flag30 = 0;
    }

    if (sharedData.height >= 35 && sharedData.height <= 45 && sharedData.Chuispeed < 0 && sharedData.flag40 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/40.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag40 = 1;
    }
    else if (sharedData.height > 45) {
        sharedData.flag40 = 0;
    }

    if (sharedData.height >= 45 && sharedData.height <= 55 && sharedData.Chuispeed < 0 && sharedData.flag50 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/50.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag50 = 1;
    }
    else if (sharedData.height > 55) {
        sharedData.flag50 = 0;
    }

    if (sharedData.height >= 55 && sharedData.height <= 65 && sharedData.Chuispeed < 0 && sharedData.flag60 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/60.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag60 = 1;
    }
    else if (sharedData.height > 65) {
        sharedData.flag60 = 0;
    }

    if (sharedData.height >= 65 && sharedData.height <= 75 && sharedData.Chuispeed < 0 && sharedData.flag70 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/70.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag70 = 1;
    }
    else if (sharedData.height > 75) {
        sharedData.flag70 = 0;
    }

    if (sharedData.height >= 75 && sharedData.height <= 85 && sharedData.Chuispeed < 0 && sharedData.flag80 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/80.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag80 = 1;
    }
    else if (sharedData.height > 85) {
        sharedData.flag80 = 0;
    }

    if (sharedData.height >= 85 && sharedData.height <= 95 && sharedData.Chuispeed < 0 && sharedData.flag90 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/90.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag90 = 1;
    }
    else if (sharedData.height > 95) {
        sharedData.flag90 = 0;
    }

    if (sharedData.height >= 95 && sharedData.height <= 120 && sharedData.Chuispeed < 0 && sharedData.flag100 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/100.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag100 = 1;
    }
    else if (sharedData.height > 120) {
        sharedData.flag100 = 0;
    }

    if (sharedData.height >= 170 && sharedData.height <= 230 && sharedData.Chuispeed < 0 && sharedData.flag200 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/200.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag200 = 1;
    }
    else if (sharedData.height > 230) {
        sharedData.flag200 = 0;
    }

    if (sharedData.height >= 270 && sharedData.height <= 330 && sharedData.Chuispeed < 0 && sharedData.flag300 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/300.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag300 = 1;
    }
    else if (sharedData.height > 330) {
        sharedData.flag300 = 0;
    }

    if (sharedData.height >= 370 && sharedData.height <= 430 && sharedData.Chuispeed < 0 && sharedData.flag400 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/400.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag400 = 1;
    }
    else if (sharedData.height > 430) {
        sharedData.flag400 = 0;
    }

    if (sharedData.height >= 470 && sharedData.height <= 530 && sharedData.Chuispeed < 0 && sharedData.flag500 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/500.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag500 = 1;
    }
    else if (sharedData.height > 530) {
        sharedData.flag500 = 0;
    }

    if (sharedData.height >= 970 && sharedData.height <= 1030 && sharedData.Chuispeed < 0 && sharedData.flag1000 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/1000.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag1000 = 1;
    }
    else if (sharedData.height > 1030) {
        sharedData.flag1000 = 0;
    }

    if (sharedData.height >= 1970 && sharedData.height <= 2030 && sharedData.Chuispeed < 0 && sharedData.flag2000 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/2000.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag2000 = 1;
    }
    else if (sharedData.height > 2030) {
        sharedData.flag2000 = 0;
    }

    if (sharedData.height >= 2470 && sharedData.height <= 2530 && sharedData.Chuispeed < 0 && sharedData.flag2500 == 0) {
        const char* cmd_open = "open \"resource/sounds.rc/2500.wav\" alias myaudio";
        mciSendStringA(cmd_open, NULL, 0, NULL);
        //从头播放
        const char* cmd_seek = "seek myaudio to start";
        mciSendStringA(cmd_seek, NULL, 0, NULL);
        const char* cmd_play = "play myaudio notify"; // 播放完成后会发送消息
        mciSendStringA(cmd_play, NULL, 0, NULL);
        sharedData.flag2500 = 1;
    }
    else if (sharedData.height > 2530) {
        sharedData.flag2500 = 0;
    }
}